package org.dromara.hm.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.hm.domain.vo.saleOrder.HmSaleOrderRefundVo;
import org.dromara.hm.domain.bo.HmSaleOrderRefundBo;
import org.dromara.hm.service.IHmSaleOrderRefundService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 退款退货明细
 *
 * @author Hzy
 * @date 2025-04-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hm/saleOrderRefund")
public class HmSaleOrderRefundController extends BaseController {

    private final IHmSaleOrderRefundService hmSaleOrderRefundService;

    /**
     * 查询退款退货明细列表
     */
    @GetMapping("/list")
    public TableDataInfo<HmSaleOrderRefundVo> list(HmSaleOrderRefundBo bo, PageQuery pageQuery) {
        return hmSaleOrderRefundService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出退款退货明细列表
     */
    @Log(title = "退款退货明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HmSaleOrderRefundBo bo, HttpServletResponse response) {
        List<HmSaleOrderRefundVo> list = hmSaleOrderRefundService.queryList(bo);
        ExcelUtil.exportExcel(list, "退款退货明细", HmSaleOrderRefundVo.class, response);
    }

    /**
     * 获取退款退货明细详细信息
     *
     * @param refundId 主键
     */
    @GetMapping("/{refundId}")
    public R<HmSaleOrderRefundVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String refundId) {
        return R.ok(hmSaleOrderRefundService.queryById(refundId));
    }

    /**
     * 新增退款退货明细
     */
    @Log(title = "退款退货明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HmSaleOrderRefundBo bo) {
        return toAjax(hmSaleOrderRefundService.insertByBo(bo));
    }

    /**
     * 修改退款退货明细
     */
    @Log(title = "退款退货明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmSaleOrderRefundBo bo) {
        return toAjax(hmSaleOrderRefundService.updateByBo(bo));
    }

    /**
     * 删除退款退货明细
     *
     * @param refundIds 主键串
     */
    @Log(title = "退款退货明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{refundIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] refundIds) {
        return toAjax(hmSaleOrderRefundService.deleteWithValidByIds(List.of(refundIds), true));
    }
}
